home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig10_01.jar / Ch10 / Fig10_01 / Fig10_01.cpp < prev    next >
C/C++ Source or Header  |  1997-10-28  |  2KB  |  65 lines

  1. // Fig. 10.1: fig10_01.cpp
  2. // Driver for Employee hierarchy
  3. #include <iostream.h>
  4. #include <iomanip.h>
  5. #include "employ2.h"
  6. #include "boss1.h"
  7. #include "commis1.h"
  8. #include "piece1.h"
  9. #include "hourly1.h"
  10.  
  11. void virtualViaPointer( const Employee * );
  12. void virtualViaReference( const Employee & );
  13.  
  14. int main()
  15. {
  16.    // set output formatting
  17.    cout << setiosflags( ios::fixed | ios::showpoint ) 
  18.         << setprecision( 2 );
  19.  
  20.    Boss b( "John", "Smith", 800.00 );
  21.    b.print();                              // static binding
  22.    cout << " earned $" << b.earnings();    // static binding
  23.    virtualViaPointer( &b );          // uses dynamic binding
  24.    virtualViaReference( b );         // uses dynamic binding
  25.  
  26.    CommissionWorker c( "Sue", "Jones", 200.0, 3.0, 150 );
  27.    c.print();                              // static binding
  28.    cout << " earned $" << c.earnings();    // static binding
  29.    virtualViaPointer( &c );          // uses dynamic binding
  30.    virtualViaReference( c );         // uses dynamic binding
  31.  
  32.    PieceWorker p( "Bob", "Lewis", 2.5, 200 );
  33.    p.print();                              // static binding
  34.    cout << " earned $" << p.earnings();    // static binding
  35.    virtualViaPointer( &p );          // uses dynamic binding
  36.    virtualViaReference( p );         // uses dynamic binding
  37.  
  38.    HourlyWorker h( "Karen", "Price", 13.75, 40 );
  39.    h.print();                              // static binding
  40.    cout << " earned $" << h.earnings();    // static binding
  41.    virtualViaPointer( &h );          // uses dynamic binding
  42.    virtualViaReference( h );         // uses dynamic binding
  43.    cout << endl;
  44.    return 0;
  45. }
  46.  
  47. // Make virtual function calls off a base-class pointer
  48. // using dynamic binding.
  49. void virtualViaPointer( const Employee *baseClassPtr )
  50. {
  51.    baseClassPtr->print();
  52.    cout << " earned $" << baseClassPtr->earnings();
  53. }
  54.  
  55. // Make virtual function calls off a base-class reference
  56. // using dynamic binding.
  57. void virtualViaReference( const Employee &baseClassRef )
  58. {
  59.    baseClassRef.print();
  60.    cout << " earned $" << baseClassRef.earnings();
  61. }
  62.  
  63.  
  64.  
  65.